package com.example.drop

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.common.view.view.BaseMvvmActivity
import com.example.drop.adapter.TaskXiangAdapter
import com.example.drop.databinding.ActivityDropXiangBinding
import com.example.drop.entity.PointEntityItem
import com.example.drop.entity.TaskEntityItem
import com.example.drop.vm.DropViewModel
import com.example.drop.vm.PointViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/drop/DropXiangActivity")
//taskId
class DropXiangActivity : BaseMvvmActivity<ActivityDropXiangBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_drop_xiang
    @Autowired
    lateinit var task:TaskEntityItem
    val taskXiangAdapter=TaskXiangAdapter()
    val pointViewModel:PointViewModel by viewModels ()
    val dropViewModel:DropViewModel by viewModels ()
    var index=0
    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        mDataBinding.ivBack.setOnClickListener {
            finish()
        }
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        mDataBinding.rv.adapter=taskXiangAdapter
        mDataBinding.taskName.text=task.taskName
        mDataBinding.patrolTime.text=task.patrolTime
        dropViewModel.getTaskById(task.id)
        dropViewModel.mSuccessLiveData.observe(this){
            it.data.forEach {
                it.patrolPoints.forEach {
                    pointViewModel.getPointById(it)
                }
                index=it.patrolPoints.size
                if(it.patrolStatus=="0"){
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_blue)
                    mDataBinding.num.text="0/${index}"
                    mDataBinding.status.setText("未巡检")
                    mDataBinding.patrolStatus.setText("未分配")
                    mDataBinding.btn1.visibility=View.VISIBLE
                }else if(it.patrolStatus=="1"){
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_ju)
                    mDataBinding.num.text="1/${index}"
                    mDataBinding.status.setText("巡检中")
                    mDataBinding.patrolStatus.setText("已分配")
                    mDataBinding.btn2.visibility=View.VISIBLE
                }else if(it.patrolStatus=="2"){
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_red)
                    mDataBinding.num.text="3/${index}"
                    mDataBinding.patrolStatus.setText("已完成")
                    mDataBinding.status.setText("已完成")
                }else{
                    mDataBinding.status.setBackgroundResource(R.drawable.bg_zhizhi)
                    mDataBinding.num.text="3/${index}"
                    mDataBinding.patrolStatus.setText("已作废")
                    mDataBinding.status.setText("已作废")
                }
            }
        }
        pointViewModel.mSuccessLiveData.observe(this){
            taskXiangAdapter.addData(it.data)
            taskXiangAdapter.notifyDataSetChanged()
        }
    }
}
